set scheme s2color

*** Summary statistics we want to map

* Elections collected
use "$project_path/data/3_cleaned/parliamentary_elections.dta", clear
keep Country Year Month Type_Election
append using "$project_path/data/3_cleaned/presidential_elections.dta"
keep Country Year Month Type_Election
gen nb_elections = 1
collapse (sum) nb_elections, by(Country)
tempfile nb_elections
save `nb_elections' 

* Elections in sample, elections with turnovers, elections in the bandwidth for the general index
use "$project_path/data/4_regdata/regdata.dta", clear
drop if runvar==.
gen nb_elections_sample = 1
gen nb_elections_turnover = (treatment==1)
collapse (sum) nb_elections_sample nb_elections_turnover, by(Country)

merge 1:1 Country using `nb_elections', nogen

foreach v in nb_elections nb_elections_sample nb_elections_turnover {
	replace `v'=. if `v'==0
}

tempfile sum_stats
save `sum_stats'

*** Merge with map

use "$project_path/data/1_input/other/maps/UIA_World_Countries_Boundaries/world.dta", clear
gen Country = COUNTRY
replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Virgin Islands, British"  if Country=="British Virgin Islands"
replace Country="Brunei"  if Country=="Brunei Darussalam"
replace Country="C. Verde Is."  if Country=="Cabo Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Congo (DRC)"  if Country=="Congo DRC"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Ivory Coast"  if Country=="Côte d'Ivoire"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="Micronesia, Federated States of"  if Country=="Micronesia"
replace Country="PRK"  if Country=="North Korea"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Russia"  if Country=="Russian Federation"
replace Country="St. Lucia"  if Country=="Saint Lucia"
replace Country="Saint Vincent and The Grenadines"  if Country=="Saint Vincent and the Grenadines"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States"
replace Country="Occupied Palestinian Territories"  if Country=="Palestinian Territory"
replace Country="France"  if Country=="French Guiana"
replace Country="Norway"  if Country=="Svalbard"

merge m:1 Country using `sum_stats', keep(master match) nogen

//We don't plot Antarctica
drop if Country=="Antarctica"

*** We plot maps

colorpalette w3 green, n(12) nograph
local colors `r(p)' 

** Appendix Figure B.2(a) -- All elections

spmap nb_elections using "$project_path/data/1_input/other/maps/UIA_World_Countries_Boundaries/world_shp" , id(_ID) clmethod(custom) clbreaks(1(5)56) ocolor(black ..) fcolor("`colors'") osize(0.04 ..) ndocolor(black ..) ndfcolor(gs10 ..) ndsize(0.04 ..) ndlabel("No elections") legend(pos(7) size(*2)) legstyle(2) title("All elections", size(vlarge)) graphregion(color(white))

graph export "$project_path/output/appendix_figures/Appendix_Figure_B2a.png", as(png) name("Graph") replace 

** Appendix Figure B.2(b) -- All elections in sample

spmap nb_elections_sample using "$project_path/data/1_input/other/maps/UIA_World_Countries_Boundaries/world_shp" , id(_ID) clmethod(custom) clbreaks(1(5)56) ocolor(black ..) fcolor("`colors'") osize(0.04 ..) ndocolor(black ..) ndfcolor(gs10 ..) ndsize(0.04 ..) ndlabel("No elections") legend(pos(7) size(*2)) legstyle(2) title("Elections in the main sample", size(vlarge)) graphregion(color(white))

graph export "$project_path/output/appendix_figures/Appendix_Figure_B2b.png", as(png) name("Graph") replace

** Appendix Figure B.2(c) -- All elections with a turnover

spmap nb_elections_turnover using "$project_path/data/1_input/other/maps/UIA_World_Countries_Boundaries/world_shp" , id(_ID) clmethod(custom) clbreaks(1(2)22) ocolor(black ..) fcolor("`colors'") osize(0.04 ..) ndocolor(black ..) ndfcolor(gs10 ..) ndsize(0.04 ..) ndlabel("No elections") legend(pos(7) size(*2)) legstyle(2) title("Elections with an electoral turnover", size(vlarge)) graphregion(color(white))

graph export "$project_path/output/appendix_figures/Appendix_Figure_B2c.png", as(png) name("Graph") replace
 
